// 加减天数
Date.prototype.addDays = function(d)
{
    this.setDate(this.getDate() + d);
};

// 加减星期
Date.prototype.addWeeks = function(w)
{
    this.addDays(w * 7);
};

// 加减月份
Date.prototype.addMonths= function(m)
{
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);

    if (this.getDate() < d)
        this.setDate(0);
};

// 加减年份
Date.prototype.addYears = function(y)
{
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);

    if (m < this.getMonth())
    {
        this.setDate(0);
    }
};

// 时间日期格式化
Date.prototype.Format = function(fmt)
{
    var o =
    {
        "M+" : this.getMonth() + 1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

    return fmt;
};

/**
* 将服务器时间格式转化为本地显示格式，例：20160105转换为2016-01-05
*/
String.prototype.changeServerToLocalFormat = function(){
    var str = this||"";

    var pattern = /(\d{4})(\d{2})(\d{2})/;
    var formatedDateString = str.replace(pattern, '$1-$2-$3');

    return formatedDateString;
};

/**
* 将服务器时间格式转化为本地显示格式，例：14043302转换为14:04:33、1404转换为14:04
* bHHMM：YES：显示小时分钟；NO：显示小时分钟秒
*/
String.prototype.changeTimeToLocalFormat = function(bHHMM){
    var str = this||"";

    if(str.length == 7||str.length==5){ //例如9304023转换为09304023
        str = "0" + str;
    }
    var bNum = 6;  // 6位以下补成 时:分:秒
    if (bHHMM) {   // 4位以下补成 时:分
        bNum = 4;
    };
    for(var i=str.length; i<bNum; i++){
        str = "0" + str;
    }

    if (bHHMM) {
        if(str.length > 4){
            str = str.substr(0, 4);
        }
    } else {
        if(str.length > 6){
            str = str.substr(0, 6);
        }
    }

    if (str.length == 4) {
        var pattern = /(\d{2})(\d{2})/;
        var formatedDateString = str.replace(pattern, '$1:$2');

        return formatedDateString;
    } else {
        var pattern = /(\d{2})(\d{2})(\d{2})/;
        var formatedDateString = str.replace(pattern, '$1:$2:$3');

        return formatedDateString;
    }
};

/**
* 去除时间中的微秒
*/
String.prototype.removeMicrosecond = function(){
    var str = this||"";

    if(str.indexOf(".")>=0){
        str = str.substr(0, str.indexOf("."));
    }

    return str;
};

var KmcDateFunction = {

    period:{ qsrq:'', zzrq:''},


    /*============================================
    函数功能:      获取起始终止时间段
    @backDate:      向前倒退的周期数
    @interval:      向前倒退的间隔（默认为月）
    函数功能  ：  period----过去到现在的时间段
    修改备注：
    日期：2017.7.21
    内容：查询最近一周内数据包含当天的，查询最近一个月内数据不包含当天的
    ============================================*/
    getStartEndPeroid:function(backMonth, interval, fromServerTime){
        var now = new Date();
        switch (interval) {
            case 'day':
                if (fromServerTime) {
                    this.period.zzrq = fromServerTime;
                    this.period.qsrq = fromServerTime;
                }else {
                    this.period.zzrq=now.Format("yyyyMMdd");
                    now = new Date();
                    now.addDays(backMonth);
                    this.period.qsrq = now.Format("yyyyMMdd");
                }
                break;
            case 'week':
                // 查询最近一周内数据包含当天的
                if (fromServerTime) {
                    this.period.zzrq = fromServerTime;
                    var ksrq = new Date(fromServerTime.slice(0, 4)+"/"+fromServerTime.slice(4, 6)+"/"+fromServerTime.slice(6, 8));
                    ksrq.addWeeks(-1);
                    this.period.qsrq = ksrq.Format("yyyyMMdd");
                }else {
                    this.period.zzrq=now.Format("yyyyMMdd");
                    now = new Date();
                    now.addWeeks(backMonth);
                    this.period.qsrq = now.Format("yyyyMMdd");
                }
                break;
            case 'month':
                //查询最近一个月数据包含当天的
                if (fromServerTime) {
                    this.period.zzrq = fromServerTime;
                    var ksrq = new Date(fromServerTime.slice(0, 4)+"/"+fromServerTime.slice(4, 6)+"/"+fromServerTime.slice(6, 8));
                    ksrq.addMonths(backMonth);
                    this.period.qsrq = ksrq.Format("yyyyMMdd");
                }else {
                    this.period.zzrq=now.Format("yyyyMMdd");
                    now = new Date();
                    now.addMonths(backMonth);
                    this.period.qsrq = now.Format("yyyyMMdd");
                }
                break;
            default:
                var now = new Date();

                // 一个月内的记录不包含当日的记录，几个月内的记录包含当月的记录
                backMonth === -1 ? now.addDays(-1):null;

                if(backMonth < -1){
                    now.addMonths(-1);
                }

                this.period.zzrq=now.Format("yyyyMMdd");

                if(backMonth == "week"){
                    now.addWeeks(-1);
                }else{
                    now.addMonths(backMonth);
                }

                this.period.qsrq=now.Format("yyyyMMdd");
        }
        return this.period;
    },

    /*============================================
    函数功能:      获取起始终止时间段  当前时间往前推的时间段
    @backDay:      向前倒退的天数
    函数功能  ：  period----过去到现在的时间段
    ============================================*/
    getStartEndPeroidWithDay:function(backDay){
        var now=new Date();

        this.period.zzrq=now.Format("yyyyMMdd");
        now.addDays(-backDay+1);//起始日期需要再加一天，才是实际的间隔天数
        this.period.qsrq=now.Format("yyyyMMdd");

        return this.period;

    },


    /*============================================
    函数功能:      获取起始终止时间段
    @backMonth:      向前前进的月份
    函数功能  ：  period----过去到现在的时间段
    ============================================*/
    getAheadStartEndPeroid:function(aheadMonth){
        var now=new Date();

        // 一个月内的记录不包含当日的记录，几个月内的记录不包含当月的记录
        aheadMonth === 1 ? now.addDays(1):null;

        if(aheadMonth > 1){
            now.addMonths(1);
        }

        this.period.zzrq=now.Format("yyyyMMdd");

        if(aheadMonth == "week"){
            now.addWeeks(1);
        }else{
            now.addMonths(aheadMonth);
        }

        this.period.qsrq=now.Format("yyyyMMdd");

        return this.period;

    },

    /**
    * 获取交易日期的时间格式，例如20151014
    * @date 传入的时间值，类型为Date
    */
    getJiaoYiDateFormat: function (date) {
        return date.Format("yyyyMMdd");
    },

    /*============================================
    函数功能:     获取当天日期 格式YYYY-MM-DD
    函数功能  ：  period----过去到现在的时间段
    ============================================*/
    getToday:function(){

        var myDate=new Date();
        var month = myDate.getMonth() + 1;
        var Day = myDate.getDate();
        var today = "";

        if(month<10){
            if(Day<10){
                today = myDate.getFullYear() + "0" + month + "0" + Day;
            }else{
                today = myDate.getFullYear() + "0" + month + "" + Day;
            }
        }
        else{
            if(Day<10){
                today = myDate.getFullYear() + "" + month + "0" + Day;
            }else{
                today = myDate.getFullYear() + "" + month + "" + Day;
            }
        }

        return today;
    },

    getDefaultYear:function(y){
        var year='';
        var myDate=new Date();
        var month = myDate.getMonth() + 1;
        var Day = myDate.getDate();

        var future=parseInt(myDate.getFullYear())+y;


        if(month<10){
            if(Day<10){
                year = future + "0" + month + "0" + Day;
            }else{
                year = future + "0" + month + "" + Day;
            }
        }
        else{
            if(Day<10){
                year = future + "" + month + "0" + Day;
            }else{
                year = future + "" + month + "" + Day;
            }
        }

        return year;
    },

    /*==================================================
     函数功能：格式化字符串日期
     入参说明：data   字符串日期
     format 格式
     ===================================================*/
    formatDate: function(date, format) {
        if (date == undefined || date == null || date == "") return;

        format = format == undefined || format == null ? "yyyyMMdd" : format;

        date = date.split("");
        format = format.split("");

        var newdate = [];
        var item = "";

        for (var key in format) {
            if (key > date.length - 1) break;

            if (!(format[key] == "y" || format[key] == "M" || format[key] == "d" || format[key] == "H" || format[key] == "h" || format[key] == "m" || format[key] == "s")) {
                date.splice(key, 0, format[key]);
            }

            newdate[key] = date[key];
        }

        // 数组日期转为字符串
        newdate = newdate.join("");

        return newdate;

    },

    getDayInWeek: function(date){
        var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        var day = (date || new Date()).getDay();
        return weekday[day];
    }
};

module.exports = KmcDateFunction;
